﻿using System;
using System.Collections.Generic;

namespace GameSdk.MsgHelper
{
    public class SdkRouteBasicHelper
    {
        protected Int32 NowReqId { get; set; } = 0;

        protected Dictionary<int, object> RouteMap { get; private set; } = new Dictionary<int, object>();
        protected Dictionary<int, Action<object, int>> CallBacks { get; private set; } = new Dictionary<int, Action<object, int>>();

        public void ExecuteAction(int reqId, object data)
        {
            if (CallBacks.ContainsKey(reqId))
            {
                CallBacks[reqId].Invoke(data, reqId);
                RemoveAction(reqId);
            }
        }

        public void RemoveAction(int reqId)
        {
            if (CallBacks.ContainsKey(reqId))
                CallBacks.Remove(reqId);
        }

        public object GetRoute(int reqId)
        {
            object tempRoute = null;
            if (RouteMap.ContainsKey(reqId))
            {
                tempRoute = RouteMap[reqId];
                RouteMap.Remove(reqId);
            }
            return tempRoute;
        }

        public void SetRouteAndAction(string route, Action<object, int> callback = null)
        {
            this.RouteMap.Add(NowReqId, route);
            if (callback != null)
                this.CallBacks.Add(NowReqId, callback);
        }

        protected Int32 NextReqId
        {
            get
            {
                this.NowReqId++;

                if (this.NowReqId > 127)
                    this.NowReqId = 1;

                return this.NowReqId;
            }
        }
    }
}